package py.patch.parse;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import py.patch.util.DateBean;
import py.patch.util.ReadUtil;

public class SvnPatch {
	public static Logger logger = Logger.getLogger(SvnPatch.class);
	
	private String projectName;
	private String projectPath;
	private String projectWebRoot;
	private boolean includeTest = false;
	private String zipOutputPath;
	
	public SvnPatch(){
		
	}
	
	public SvnPatch(String projectName, String projectPath,
			String projectWebRoot,String zipOutputPath) {
		super();
		this.projectName = projectName;
		this.projectPath = projectPath;
		this.projectWebRoot = projectWebRoot;
		
		zipOutputPath = zipOutputPath.replace("/", File.separator).replace("\\",File.separator);
		if(zipOutputPath.endsWith(File.separator)){
			zipOutputPath = zipOutputPath.substring(0,zipOutputPath.length()-2);
		}
		
		this.zipOutputPath = zipOutputPath +"-"+ DateBean.getSysdateTime();
	}

	public SvnPatch(String projectName, String projectPath,
			String projectWebRoot,boolean includeTest,String zipOutputPath) {
		super();
		this.projectName = projectName;
		this.projectPath = projectPath;
		this.projectWebRoot = projectWebRoot;
		this.setIncludeTest(includeTest);
		this.zipOutputPath = zipOutputPath;
	}


	public String getProjectName() {
		return projectName;
	}



	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}



	public String getProjectPath() {
		return projectPath;
	}



	public void setProjectPath(String projectPath) {
		this.projectPath = projectPath;
	}



	public String getProjectWebRoot() {
		return projectWebRoot;
	}



	public void setProjectWebRoot(String projectWebRoot) {
		this.projectWebRoot = projectWebRoot;
	}


	public Map<Integer,String> getRealDeployPathsMap(String fileName){
		Map<Integer, String> map =  ReadUtil.readFileByLines(fileName);
		Map<Integer, String> newMap = new HashMap<Integer, String>();
		for (@SuppressWarnings("rawtypes")
		Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
			Integer key = (Integer)iterator.next();
			String value = map.get(key);
			value = value.trim();
			if(value.startsWith("#")){
				continue;
			}
			
			if(value.indexOf(this.getProjectName()) >= 1){
				 value = value.substring(value.indexOf(this.getProjectName())+this.getProjectName().length());
				 
				 if(value.startsWith("/src")){
					 value = value.replace("/src/", "/"+this.getProjectWebRoot()+"/WEB-INF/classes/");
					 if(value.endsWith(".java")){
						 value = value.replace(".java", ".class");
					 }
				 }
				 if(value.startsWith("/")){
					 value = this.getProjectPath()+value;
				 }else{
					 value = this.getProjectPath()+File.separator+ value;
				 }
				 
				 value = value.replace("/", File.separator).replace("\\",File.separator);
				 
				 if(!this.includeTest){
					 if(value.contains(File.separator+"test"+File.separator)){
					 }else{
						 newMap.put(key,value);
					 }
				 }else{
					 newMap.put(key,value);
				 }
			}
			
		}
		
	
		return newMap;
	}
	
	
	public void svnPathGenerate(String fileName){
		
		Map<Integer, String> map = this.getRealDeployPathsMap(fileName);
		
		//首先清空之前的内容
		//ReadUtil.deleteDir(new File(svnPath.getZipOutputPath()));
		
		logger.debug("**********************************************************");
		logger.debug("**********************************************************");
		logger.debug("拷贝文件目标路径："+this.getZipOutputPath());
		logger.debug("拷贝的文件列表：");
		logger.debug("包不包含测试类:"+this.isIncludeTest());
		for (Integer key : map.keySet()) {
			String value = map.get(key);
			logger.debug("key:"+key+"    value:"+value);
			String destPath = "";
			int proIndex = value.indexOf(this.getProjectName());
			String destFullPathPrefix = this.getZipOutputPath();
			if(!this.getZipOutputPath().endsWith("/") ||this.getZipOutputPath().endsWith(File.separator) ){
				destFullPathPrefix = this.getZipOutputPath()+File.separator;
			}
			destPath = destFullPathPrefix + value.substring(proIndex);
			destPath = destPath.replace("/", File.separator).replace("\\",File.separator);
			destPath = destPath.replace(this.getProjectWebRoot()+File.separator, "");
			try {
				if(new File(value).isDirectory()){
					logger.warn("------目录不用拷贝----"+value);
					continue;
				}
				ReadUtil.copyFile(value, destPath);
			} catch (Exception e) {
				logger.error(e);
				e.printStackTrace();
			}
		}
	}

	public boolean isIncludeTest() {
		return includeTest;
	}

	public void setIncludeTest(boolean includeTest) {
		this.includeTest = includeTest;
	}

	public String getZipOutputPath() {
		return zipOutputPath;
	}

	public void setZipOutputPath(String zipOutputPath) {
		this.zipOutputPath = zipOutputPath;
	}
}
